import prompt from '@system.prompt';
import router from '@system.router';
import inputMethod from '@ohos.inputMethod'

export default {
    data: {
        question: "",
        deviceList: []
    },
    inputChanged({value}) {
        this.question = value
    },
    send() {
        if (this.question.length < 6) {
            prompt.showToast({
                message: "您的提问少于6个字，请重新输入",
                duration: 2000
            })
        } else {
            this.getDeviceList();
        }

        console.log(JSON.stringify(this.deviceList))
    },
    getDeviceList: async function () {
        let ret = await FeatureAbility.getDeviceList(1);
        if (ret.code == 0) {
            this.deviceList = ret.data.filter(t => t.deviceType == "SMART_PHONE" || t.deviceType == "SMART_WATCH")
            this.showDialog();
        } else {
            prompt.showToast({
                message: "当前网络下无可流转设备",
                duration: 2000
            })
        }
    },
    showDialog() {
        this.$element('deviceList').show()
    },
    hideDialog() {
        this.$element('deviceList').close()
    },
    start: async function (deviceId) {
        let data = {
            question: this.question
        }
        let target = {
            bundleName: "com.panda_coder.liedetector",
            abilityName: "com.panda_coder.linedetector.watch.MainAbility",
            networkId: deviceId,
            url: "pages/question/question",
            data: data
        };

        let result = await FeatureAbility.startAbility(target);
        if (result.code == 0) {
            this.hideDialog()
            this.goMonitorPanel()
        } else {
            prompt.showToast({
                message: "程序开小差了,请检查当前设备能否流转后重试",
                duration: 2000
            })
        }
    },
    deviceChecked({networkId}) {
        var InputMethodController = inputMethod.getInputMethodController();
        InputMethodController.stopInput()
        this.start(networkId)
    },
    goMonitorPanel() {
        router.push({
            uri: "pages/monitorpanel/monitorpanel"
        })
    }
}
